package artibella.media
{
	/**
	 * ...
	 * @author pbordachar
	 * 
	 */
	
	public class AList
	{
		protected var _looping:Boolean;
		
		protected var _items:Array;
		protected var _currentIndice:int = -1;
		
		public function AList( loop:Boolean = false ) 
		{
			_items = new Array();
			_looping = loop;
		}
		
		public function addItem( s:AListItem ):void
		{
			for ( var i:int = 0;  i < _items.length; i ++ )
			{
				if ( s == _items[ i ] )
				{
					return;
				}
			}
			
			s.indice = _items.length;
			
			_items.push( s );
		}
		
		// - - -
		
		public function getItemByIndex( i:int ):AListItem
		{
			if ( i < _items.length )
			{
				_currentIndice = i;
				return _items[ _currentIndice ];
			}
			
			return null;
		}
		
		public function getNextItem():AListItem
		{
			if ( _looping ) 
			{
				if ( _currentIndice == _items.length - 1 )
				{
					_currentIndice = 0;
					return _items[ _currentIndice ];
				}
				else 
				{
					_currentIndice ++;
					return _items[ _currentIndice ];
				}
				
			} else {
				
				if ( _currentIndice < _items.length - 1 ) {
					_currentIndice ++;
					return _items[ _currentIndice ];
				}
				else 
				{
					return null;
				}
			}
		}
		
		public function getPreviousItem():AListItem
		{
			if ( _looping ) 
			{
				if ( _currentIndice == 0 )
				{
					_currentIndice = _items.length - 1;
					return _items[ _currentIndice ];
				} 
				else
				{
					_currentIndice --;
					return _items[ _currentIndice ];
				}
				
			} else {
				
				if ( _currentIndice > 0 ) {
					_currentIndice --;
					return _items[ _currentIndice ];
				}
			}
			
			return null;
		}
		
		// - - -
		
		public function get items():Array
		{
			return _items;
		}
		
	}

}